﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Project.MODEL;
using Project.DAL;
using System.Security.Cryptography;
using Newtonsoft.Json;

namespace Project.BLL
{
    public class ManageBll
    {
        ManageDal dal = new ManageDal();

        /// <summary>
        /// 登录获取实体
        /// </summary>
        /// <param name="loginid">账户</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public Employee GetEmployeeByLogin(string loginid,string pwd) {
            var emps = dal.GetEmployees();
            string md5pwd = md5(pwd);
            var emp = emps.Where(s => s.loginID == loginid && s.pwd == md5pwd).FirstOrDefault();
            return emp;
        }

        /// <summary>
        /// 获取用户菜单列表
        /// </summary>
        /// <param name="rid">角色ID</param>
        /// <returns></returns>
        public string GetMenusByRid(int rid) {

            var authoritys = dal.GetAuthorities();

            var auths = authoritys.Where(a => a.roleID == rid).OrderBy(a=>a.sort);

            var list = from a in auths
                       select new
                       {
                           a.a_Id,
                           a.menuID,
                           a.Menus.menuName,
                           a.Menus.menuUrl,
                           a.Menus.areaName,
                           a.Menus.controllerName,
                           a.Menus.actionName,
                           a.Menus.parentID,
                           a.Menus.icon
                       };
            return JsonConvert.SerializeObject(list);


        }


        /// <summary>
        /// MD5加密密码
        /// </summary>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public string md5(string pwd)
        {
            MD5 md5 = MD5.Create();
            return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(pwd))).Replace("-", null);//加密输入进来的密码
        }

    }
}
